﻿using LeaderCCSLaserUI.Data;
using LeaderCCSLaserUI.Models;
using Microsoft.EntityFrameworkCore;
using Prism.Commands;
using Prism.Common;
using Prism.Services.Dialogs;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MessageBox = System.Windows.Forms.MessageBox;

namespace LeaderCCSLaserUI.ViewModels.Dialogs.VisionProgram
{
    public class ProgramManageDialogViewModel : DialogViewModel
    {
        #region 变量
        private string partNum = string.Empty;
        #endregion
        #region 属性
        private string newProgramName;
        public string NewProgramName
        {
            get { return newProgramName; }
            set { SetProperty(ref newProgramName, value); }
        }
        private bool isCopy;
        public bool IsCopy
        {
            get { return isCopy; }
            set { SetProperty(ref isCopy, value); }
        }
        private ObservableCollection<VisionProgramDisp> programList1 = new ObservableCollection<VisionProgramDisp>();
        public ObservableCollection<VisionProgramDisp> ProgramList1
        {
            get { return programList1; }
            set { SetProperty(ref programList1, value); }
        }
        #endregion
        #region 函数
        private DelegateCommand<object> addProgramCommand;
        public DelegateCommand<object> AddProgramCommand =>
            addProgramCommand ?? (addProgramCommand = new DelegateCommand<object>(ExecuteAddProgramCommand));
        private DelegateCommand<object> deleteProgramCommand;
        public DelegateCommand<object> DeleteProgramCommand =>
            deleteProgramCommand ?? (deleteProgramCommand = new DelegateCommand<object>(ExecuteDeleteProgramCommand));

        void ExecuteDeleteProgramCommand(object obj)
        {
            if (obj is VisionProgramDisp disp)
            {
                if (MessageBox.Show($"确认删除\"{disp.Name}\"程序吗？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes)
                {
                    using var mdb = new MDbContext(partNum);
                    mdb.Database.ExecuteSqlRaw($"DELETE FROM VisionPrograms WHERE Id = {disp.Id}");
                    mdb.Database.ExecuteSqlRaw($"DELETE FROM VisionProjects WHERE ProgramId = {disp.Id}");
                    mdb.SaveChanges();
                    LoadProgram(partNum);
                }
            }
            else
            {
                MessageBox.Show("未选择任何程序", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        void ExecuteAddProgramCommand(object obj)
        {
            if (NewProgramName != string.Empty)
            {
                try
                {
                    using var mdb = new MDbContext(partNum);
                    mdb.VisionPrograms.Add(new Data.VisionProgram { 
                        Name = NewProgramName
                    });
                    if (IsCopy)
                    {
                        //复制
                        if (obj is VisionProgramDisp disp)
                        {
                            mdb.SaveChanges();
                            var projects = mdb.VisionProjects.Where(p => p.ProgramId == disp.Id).OrderBy(p => p.PcsOrder);
                            if (projects.Any())
                            {
                                var _programs = mdb.VisionPrograms.Where(p => p.Name == NewProgramName).OrderByDescending(p => p.Id);
                                if (_programs.Any())
                                {
                                    var arr = _programs.ToArray();
                                    projects.ToList().ForEach(p =>
                                    {
                                        mdb.VisionProjects.Add(new VisionProject
                                        {
                                            ProgramId = arr[0].Id,
                                            FunctionId = p.FunctionId,
                                            PcsOrder = p.PcsOrder,
                                            Parameter = p.Parameter
                                        });
                                    });
                                    mdb.SaveChanges();
                                }
                            }
                            MessageBox.Show($"程序\"{NewProgramName}\"添加完成", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            LoadProgram(partNum);
                        }
                        else
                        {
                            MessageBox.Show("未选择任何程序", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    else
                    {
                        mdb.SaveChanges();
                        MessageBox.Show($"程序\"{NewProgramName}\"添加完成", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        LoadProgram(partNum);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                
            }
            else
            {
                MessageBox.Show("程序名不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion
        #region 导航
        public override void OnDialogOpened(IDialogParameters parameters)
        {
            partNum = parameters.GetValue<string>("PartNum");
            LoadProgram(partNum);
        }
        #endregion
        #region 构造函数
        public ProgramManageDialogViewModel(IContainerProvider containerProvider) : base(containerProvider)
        {
            Title = "程序管理";
            NewProgramName = string.Empty;
            IsCopy = false;
        }
        #endregion
        #region 功能函数
        private void LoadProgram(string partNum)
        {
            ProgramList1.Clear();
            try
            {
                using var mdb = new MDbContext(partNum);
                var programs = mdb.VisionPrograms.Where(p => p.Id > 0);
                if (programs.Any())
                {
                    programs.ToList().ForEach(p =>
                    {
                        ProgramList1.Add(new VisionProgramDisp
                        {
                            Id = p.Id,
                            Name = p.Name
                        });
                    });
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion
    }
}
